package com.hxgis.common.service.impl;

import com.es.common.EsAggCondition;
import com.es.common.domain.EsCondition;
import com.es.common.domain.agg.Group;
import com.es.common.enums.Order;
import com.es.wrapper.DataWrapper;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.dto.RainInfoAnalysDTO;
import com.hxgis.common.entity.Station;
import com.hxgis.common.entity.StationExtremeInfo;
import com.hxgis.common.enums.RainFlagEnum;
import com.hxgis.common.repository.RainDayInfoRepository;
import com.hxgis.common.repository.StationExtremeInfoRepository;
import com.hxgis.common.service.StationExtremeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class StationExtremeServiceImpl implements StationExtremeService {


    @Autowired
    StationExtremeInfoRepository stationExtremeInfoRepository;

    @Autowired
    RainDayInfoRepository dayInfoRepository;

    @Override
    public void saveExtremeInfo(String startTime, String endTime, List<String> stationNoList) {

        //站点基础信息
//        List<Station> stations = StationCache.getStations();
        List<StationExtremeInfo> stationExtremeInfos = new ArrayList<>();
        //连续降水阈值
        Map<String, Integer> continueInfoDTOS = getContinueInfoDTOS(startTime, endTime, "20", 3);
        Map<String, Integer> continueInfoDTOS08 = getContinueInfoDTOS(startTime, endTime, "08", 3);
        //连续降水持续天数阈值
        Map<String, Integer> continueDayDTOS = getContinueInfoDayDTOS(startTime, endTime, "20", 3);
        Map<String, Integer> continueDayDTOS08 = getContinueInfoDayDTOS(startTime, endTime, "08", 3);
        //连续无降水天数阈值
        Map<String, Integer> notContinueDayDTOS = getNotContinueInfoDayDTOS(startTime, endTime, "20", 3);
        Map<String, Integer> notContinueDayDTOS08 = getNotContinueInfoDayDTOS(startTime, endTime, "08", 3);



        //极端日降水量阈值
        Map<String, Integer> extremeEventRain1Map = getExtremeEventRainDTOS(startTime, endTime, "20", 1, 3);
        Map<String, Integer> extremeEventRain2Map = getExtremeEventRainDTOS(startTime, endTime, "20", 2, 3);
        Map<String, Integer> extremeEventRain3Map = getExtremeEventRainDTOS(startTime, endTime, "20", 3, 3);
        Map<String, Integer> extremeEventRain4Map = getExtremeEventRainDTOS(startTime, endTime, "20", 4, 3);
        Map<String, Integer> extremeEventRain5Map = getExtremeEventRainDTOS(startTime, endTime, "20", 5, 3);
        Map<String, Integer> extremeEventRain6Map = getExtremeEventRainDTOS(startTime, endTime, "20", 6, 3);
        Map<String, Integer> extremeEventRain7Map = getExtremeEventRainDTOS(startTime, endTime, "20", 7, 3);
        Map<String, Integer> extremeEventRain8Map = getExtremeEventRainDTOS(startTime, endTime, "20", 8, 3);
        Map<String, Integer> extremeEventRain9Map = getExtremeEventRainDTOS(startTime, endTime, "20", 9, 3);
        Map<String, Integer> extremeEventRain10Map = getExtremeEventRainDTOS(startTime, endTime, "20", 10, 3);
        Map<String, Integer> extremeEventRain11Map = getExtremeEventRainDTOS(startTime, endTime, "20", 11, 3);
        Map<String, Integer> extremeEventRain12Map = getExtremeEventRainDTOS(startTime, endTime, "20", 12, 3);
        Map<String, Integer> extremeEventRain13Map = getExtremeEventRainDTOS(startTime, endTime, "20", 13, 3);
        Map<String, Integer> extremeEventRain14Map = getExtremeEventRainDTOS(startTime, endTime, "20", 14, 3);
        Map<String, Integer> extremeEventRain15Map = getExtremeEventRainDTOS(startTime, endTime, "20", 15, 3);

        Map<String, Integer> extreme08EventRain1Map = getExtremeEventRainDTOS(startTime, endTime, "08", 1, 3);
        Map<String, Integer> extreme08EventRain2Map = getExtremeEventRainDTOS(startTime, endTime, "08", 2, 3);
        Map<String, Integer> extreme08EventRain3Map = getExtremeEventRainDTOS(startTime, endTime, "08", 3, 3);
        Map<String, Integer> extreme08EventRain4Map = getExtremeEventRainDTOS(startTime, endTime, "08", 4, 3);
        Map<String, Integer> extreme08EventRain5Map = getExtremeEventRainDTOS(startTime, endTime, "08", 5, 3);
        Map<String, Integer> extreme08EventRain6Map = getExtremeEventRainDTOS(startTime, endTime, "08", 6, 3);
        Map<String, Integer> extreme08EventRain7Map = getExtremeEventRainDTOS(startTime, endTime, "08", 7, 3);
        Map<String, Integer> extreme08EventRain8Map = getExtremeEventRainDTOS(startTime, endTime, "08", 8, 3);
        Map<String, Integer> extreme08EventRain9Map = getExtremeEventRainDTOS(startTime, endTime, "08", 9, 3);
        Map<String, Integer> extreme08EventRain10Map = getExtremeEventRainDTOS(startTime, endTime, "08", 10, 3);
        Map<String, Integer> extreme08EventRain11Map = getExtremeEventRainDTOS(startTime, endTime, "08", 11, 3);
        Map<String, Integer> extreme08EventRain12Map = getExtremeEventRainDTOS(startTime, endTime, "08", 12, 3);
        Map<String, Integer> extreme08EventRain13Map = getExtremeEventRainDTOS(startTime, endTime, "08", 13, 3);
        Map<String, Integer> extreme08EventRain14Map = getExtremeEventRainDTOS(startTime, endTime, "08", 14, 3);
        Map<String, Integer> extreme08EventRain15Map = getExtremeEventRainDTOS(startTime, endTime, "08", 15, 3);


        //循环全站点信息
        stationNoList.stream().forEach(x -> {
            StationExtremeInfo extremeEventDTO = new StationExtremeInfo();
            extremeEventDTO.setStationNo(x);
            extremeEventDTO.setRainThreshold20_1(Objects.isNull(extremeEventRain1Map.get(x)) ? 0 : extremeEventRain1Map.get(x));
            extremeEventDTO.setRainThreshold20_2(Objects.isNull(extremeEventRain2Map.get(x)) ? 0 : extremeEventRain2Map.get(x));
            extremeEventDTO.setRainThreshold20_3(Objects.isNull(extremeEventRain3Map.get(x)) ? 0 : extremeEventRain3Map.get(x));
            extremeEventDTO.setRainThreshold20_4(Objects.isNull(extremeEventRain4Map.get(x)) ? 0 : extremeEventRain4Map.get(x));
            extremeEventDTO.setRainThreshold20_5(Objects.isNull(extremeEventRain5Map.get(x)) ? 0 : extremeEventRain5Map.get(x));
            extremeEventDTO.setRainThreshold20_6(Objects.isNull(extremeEventRain6Map.get(x)) ? 0 : extremeEventRain6Map.get(x));
            extremeEventDTO.setRainThreshold20_7(Objects.isNull(extremeEventRain7Map.get(x)) ? 0 : extremeEventRain7Map.get(x));
            extremeEventDTO.setRainThreshold20_8(Objects.isNull(extremeEventRain8Map.get(x)) ? 0 : extremeEventRain8Map.get(x));
            extremeEventDTO.setRainThreshold20_9(Objects.isNull(extremeEventRain9Map.get(x)) ? 0 : extremeEventRain9Map.get(x));
            extremeEventDTO.setRainThreshold20_10(Objects.isNull(extremeEventRain10Map.get(x)) ? 0 : extremeEventRain10Map.get(x));
            extremeEventDTO.setRainThreshold20_11(Objects.isNull(extremeEventRain11Map.get(x)) ? 0 : extremeEventRain11Map.get(x));
            extremeEventDTO.setRainThreshold20_12(Objects.isNull(extremeEventRain12Map.get(x)) ? 0 : extremeEventRain12Map.get(x));
            extremeEventDTO.setRainThreshold20_13(Objects.isNull(extremeEventRain13Map.get(x)) ? 0 : extremeEventRain13Map.get(x));
            extremeEventDTO.setRainThreshold20_14(Objects.isNull(extremeEventRain14Map.get(x)) ? 0 : extremeEventRain14Map.get(x));
            extremeEventDTO.setRainThreshold20_15(Objects.isNull(extremeEventRain15Map.get(x)) ? 0 : extremeEventRain15Map.get(x));

            extremeEventDTO.setRainThreshold08_1(Objects.isNull(extreme08EventRain1Map.get(x)) ? 0 : extreme08EventRain1Map.get(x));
            extremeEventDTO.setRainThreshold08_2(Objects.isNull(extreme08EventRain2Map.get(x)) ? 0 : extreme08EventRain2Map.get(x));
            extremeEventDTO.setRainThreshold08_3(Objects.isNull(extreme08EventRain3Map.get(x)) ? 0 : extreme08EventRain3Map.get(x));
            extremeEventDTO.setRainThreshold08_4(Objects.isNull(extreme08EventRain4Map.get(x)) ? 0 : extreme08EventRain4Map.get(x));
            extremeEventDTO.setRainThreshold08_5(Objects.isNull(extreme08EventRain5Map.get(x)) ? 0 : extreme08EventRain5Map.get(x));
            extremeEventDTO.setRainThreshold08_6(Objects.isNull(extreme08EventRain6Map.get(x)) ? 0 : extreme08EventRain6Map.get(x));
            extremeEventDTO.setRainThreshold08_7(Objects.isNull(extreme08EventRain7Map.get(x)) ? 0 : extreme08EventRain7Map.get(x));
            extremeEventDTO.setRainThreshold08_8(Objects.isNull(extreme08EventRain8Map.get(x)) ? 0 : extreme08EventRain8Map.get(x));
            extremeEventDTO.setRainThreshold08_9(Objects.isNull(extreme08EventRain9Map.get(x)) ? 0 : extreme08EventRain9Map.get(x));
            extremeEventDTO.setRainThreshold08_10(Objects.isNull(extreme08EventRain10Map.get(x)) ? 0 : extreme08EventRain10Map.get(x));
            extremeEventDTO.setRainThreshold08_11(Objects.isNull(extreme08EventRain11Map.get(x)) ? 0 : extreme08EventRain11Map.get(x));
            extremeEventDTO.setRainThreshold08_12(Objects.isNull(extreme08EventRain12Map.get(x)) ? 0 : extreme08EventRain12Map.get(x));
            extremeEventDTO.setRainThreshold08_13(Objects.isNull(extreme08EventRain13Map.get(x)) ? 0 : extreme08EventRain13Map.get(x));
            extremeEventDTO.setRainThreshold08_14(Objects.isNull(extreme08EventRain14Map.get(x)) ? 0 : extreme08EventRain14Map.get(x));
            extremeEventDTO.setRainThreshold08_15(Objects.isNull(extreme08EventRain15Map.get(x)) ? 0 : extreme08EventRain15Map.get(x));

            //todo 还需要跑连续降水日 连续降水量 连续无降水日  阈值信息
            extremeEventDTO.setContinueThresholdRain(Objects.isNull(continueInfoDTOS.get(x)) ? 0 : continueInfoDTOS.get(x));
            extremeEventDTO.setContinueThresholdRain_08(Objects.isNull(continueInfoDTOS08.get(x)) ? 0 : continueInfoDTOS08.get(x));
            extremeEventDTO.setContinueThresholdDay(Objects.isNull(continueDayDTOS.get(x)) ? 0 : continueDayDTOS.get(x));
            extremeEventDTO.setContinueThresholdDay_08(Objects.isNull(continueDayDTOS08.get(x)) ? 0 : continueDayDTOS08.get(x));
            extremeEventDTO.setNotContinueThresholdDay(Objects.isNull(notContinueDayDTOS.get(x)) ? 0 : notContinueDayDTOS.get(x));
            extremeEventDTO.setNotContinueThresholdDay_08(Objects.isNull(notContinueDayDTOS08.get(x)) ? 0 : notContinueDayDTOS08.get(x));

            stationExtremeInfos.add(extremeEventDTO);

        });

        stationExtremeInfoRepository.saveBatch(stationExtremeInfos);






        log.info("阈值保存完成");

    }


    @Override
    public List<StationExtremeInfo> getAllExtreInfo() {
        EsCondition esCondition = EsCondition.getEsCondition();
        esCondition.size(10000);
        return stationExtremeInfoRepository.selectList(esCondition);
    }


    //连续降水量阈值
    private Map<String, Integer> getContinueInfoDTOS(String startTime, String endTime, String flag, Integer index) {
        //查询所有的站点信息
        List<Station> stations = StationCache.getStations();
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = new ArrayList<>();
        //只能循环去查30年的数据了，因为基础信息表的数据太多 一次查所有的会超过es设置的最大数据量
        for (Station station : stations) {
            EsAggCondition esAggCondition = EsAggCondition.getInstance();
            Group stationNoGroup = esAggCondition.Group("stationNo");
            stationNoGroup.size(10000);
            Group yearGroup = stationNoGroup.groupSub("year");
            yearGroup.size(30);
            Group courseGroup = null;
            if (Objects.equals(flag,"20")){
                courseGroup = yearGroup.groupSub("continueRainFll");
                courseGroup.count("continueRainFll","continueDay");
            }
            if (Objects.equals(flag,"08")){
                courseGroup = yearGroup.groupSub("continueRainFll08");
                courseGroup.count("continueRainFll08","continueDay");
            }
            courseGroup.min("time");
            courseGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
            courseGroup.order(Order.DESC,"rainFallSum");
            courseGroup.size(2);
            EsCondition condition = esAggCondition.getCondition();
            if (Objects.equals(flag,"08")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("continueRainFll08",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(flag,"20")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("continueRainFll",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }
            condition.eq("stationNo",station.getStationNo());
            List<RainInfoAnalysDTO> rainInfoAnalysDTOSub = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
                @Override
                public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
//                if(Objects.equals(data.get("continueDay").toString(),"1") ){
//                    return null;
//                }
                    RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                    rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                    if (Objects.equals(flag,"20")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll").toString());
                    }else if (Objects.equals(flag,"08")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll08").toString());
                    }
                    rainInfoAnalysDTO.setYear(data.get("year").toString());
                    rainInfoAnalysDTO.setTime(String.valueOf(new Double(data.get("time").toString()).intValue()));
//                rainInfoAnalysDTO.setTime(String.valueOf(new BigDecimal(data.get("time").toString()).intValue()));
                    rainInfoAnalysDTO.setCountDays(Double.valueOf(data.get("continueDay").toString()));
                    rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString()));
                    return rainInfoAnalysDTO;
                }
            });
            rainInfoAnalysDTOS.addAll(rainInfoAnalysDTOSub);
        }
        Map<String, List<RainInfoAnalysDTO>> continueSumMaps = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        //站点id -》 对应的值
        Map<String, Integer> map = new HashMap<>();
        continueSumMaps.forEach((stationNo,values) -> {
            //按照时间排序返回前端
            Collections.sort(values,((o1, o2) -> {
                return (int)(o2.getRainFallSum() -o1.getRainFallSum());
            }));
            map.put(stationNo, new Double(values.get(index - 1).getRainFallSum()).intValue());
        });
        return map;
    }

    //连续降水日阈值
    private Map<String, Integer> getContinueInfoDayDTOS(String startTime, String endTime, String flag, Integer index) {


        //查询所有的站点信息
        List<Station> stations = StationCache.getStations();
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = new ArrayList<>();
        //只能循环去查30年的数据了，因为基础信息表的数据太多 一次查所有的会超过es设置的最大数据量
        for (Station station : stations) {
            EsAggCondition esAggCondition = EsAggCondition.getInstance();
            Group stationNoGroup = esAggCondition.Group("stationNo");
            stationNoGroup.size(10000);
            Group yearGroup = stationNoGroup.groupSub("year");
            yearGroup.size(30);
            Group courseGroup = null;
            if (Objects.equals(flag,"20")){
                courseGroup = yearGroup.groupSub("continueRainFll");
                courseGroup.count("continueRainFll","continueDay");
            }
            if (Objects.equals(flag,"08")){
                courseGroup = yearGroup.groupSub("continueRainFll08");
                courseGroup.count("continueRainFll08","continueDay");
            }
            courseGroup.min("time");
            courseGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
            courseGroup.order(Order.DESC,"continueDay");
            courseGroup.size(2);
            EsCondition condition = esAggCondition.getCondition();
            if (Objects.equals(flag,"08")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("continueRainFll08",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(flag,"20")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("continueRainFll",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }
            condition.eq("stationNo",station.getStationNo());
            List<RainInfoAnalysDTO> rainInfoAnalysDTOSub = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
                @Override
                public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
//                if(Objects.equals(data.get("continueDay").toString(),"1") ){
//                    return null;
//                }
                    RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                    rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                    if (Objects.equals(flag,"20")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll").toString());
                    }else if (Objects.equals(flag,"08")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll08").toString());
                    }
                    rainInfoAnalysDTO.setYear(data.get("year").toString());
                    rainInfoAnalysDTO.setTime(String.valueOf(new Double(data.get("time").toString()).intValue()));
                    rainInfoAnalysDTO.setCountDays(Double.valueOf(data.get("continueDay").toString()));
//                    rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
                    return rainInfoAnalysDTO;
                }
            });
            rainInfoAnalysDTOS.addAll(rainInfoAnalysDTOSub);
        }
        Map<String, List<RainInfoAnalysDTO>> continueSumMaps = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        //站点id -》 对应的值
        Map<String, Integer> map = new HashMap<>();
        continueSumMaps.forEach((stationNo,values) -> {
            //按照时间排序返回前端
            Collections.sort(values,((o1, o2) -> {
                return (int)(o2.getRainFallSum() -o1.getRainFallSum());
            }));
            map.put(stationNo, new Double(values.get(index - 1).getCountDays()).intValue());
        });
        return map;

    }
    //连续无降水日阈值
    private Map<String, Integer> getNotContinueInfoDayDTOS(String startTime, String endTime, String flag, Integer index) {


        //查询所有的站点信息
        List<Station> stations = StationCache.getStations();
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = new ArrayList<>();
        //只能循环去查30年的数据了，因为基础信息表的数据太多 一次查所有的会超过es设置的最大数据量
        for (Station station : stations) {
            EsAggCondition esAggCondition = EsAggCondition.getInstance();
            Group stationNoGroup = esAggCondition.Group("stationNo");
            stationNoGroup.size(10000);
            Group yearGroup = stationNoGroup.groupSub("year");
            yearGroup.size(30);
            Group courseGroup = null;
            if (Objects.equals(flag,"20")){
                courseGroup = yearGroup.groupSub("notContinueRainFll");
                courseGroup.count("notContinueRainFll","continueDay");
            }
            if (Objects.equals(flag,"08")){
                courseGroup = yearGroup.groupSub("notContinueRainFll08");
                courseGroup.count("notContinueRainFll08","continueDay");
            }
            courseGroup.min("time");
            courseGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
            courseGroup.order(Order.DESC,"continueDay");
            courseGroup.size(2);
            EsCondition condition = esAggCondition.getCondition();
            if (Objects.equals(flag,"08")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("notContinueRainFll08",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(flag,"20")){
                condition.gte("time",startTime).lte(endTime)
                        .eq("notContinueRainFll",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }
            condition.eq("stationNo",station.getStationNo());
            List<RainInfoAnalysDTO> rainInfoAnalysDTOSub = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
                @Override
                public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
//                if(Objects.equals(data.get("continueDay").toString(),"1") ){
//                    return null;
//                }
                    RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                    rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                    if (Objects.equals(flag,"20")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("notContinueRainFll").toString());
                    }else if (Objects.equals(flag,"08")){
                        rainInfoAnalysDTO.setCourseFlag(data.get("notContinueRainFll08").toString());
                    }
                    rainInfoAnalysDTO.setYear(data.get("year").toString());
                    rainInfoAnalysDTO.setTime(String.valueOf(new Double(data.get("time").toString()).intValue()));
                    rainInfoAnalysDTO.setCountDays(Double.valueOf(data.get("continueDay").toString()));
//                    rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
                    return rainInfoAnalysDTO;
                }
            });
            rainInfoAnalysDTOS.addAll(rainInfoAnalysDTOSub);
        }
        Map<String, List<RainInfoAnalysDTO>> continueSumMaps = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        //站点id -》 对应的值
        Map<String, Integer> map = new HashMap<>();
        continueSumMaps.forEach((stationNo,values) -> {
            //按照时间排序返回前端
            Collections.sort(values,((o1, o2) -> {
                return (int)(o2.getRainFallSum() -o1.getRainFallSum());
            }));
            map.put(stationNo, new Double(values.get(index - 1).getCountDays()).intValue());
        });
        return map;
    }

    private Map<String, Integer> getExtremeEventRainDTOS(String startTime, String endTime, String flag, Integer day, Integer index) {
        //根据站点分组
        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        Group stationNoGroup = esAggCondition.Group("stationNo");
        stationNoGroup.size(10000);
        Group yearGroup = stationNoGroup.groupSub("year");
        yearGroup.size(30);
        Group timeGroup = yearGroup.groupSub("time");
        if (Objects.equals(day, 1)){
            timeGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
        }else{
            timeGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage()+"_"+day,"rainFallSum");
        }
        timeGroup.order(Order.DESC,"rainFallSum");
        timeGroup.size(2);
        EsCondition condition = esAggCondition.getCondition();
        condition.gte("time",startTime).lte(endTime)
                .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
            @Override
            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                rainInfoAnalysDTO.setYear(data.get("year").toString());
                rainInfoAnalysDTO.setTime(data.get("time").toString());
                rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString()));
                return rainInfoAnalysDTO;
            }
        });

        Map<String,List<RainInfoAnalysDTO>> stationExtremeMap = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        //站点id -》 对应的值
        Map<String, Integer> map = new HashMap<>();
        stationExtremeMap.forEach((stationNo,values) -> {
            //按照时间排序返回前端
            Collections.sort(values,((o1, o2) -> {
                return (int)(o2.getRainFallSum() -o1.getRainFallSum());
            }));
            map.put(stationNo, new Double(values.get(index - 1).getRainFallSum()).intValue());
        });
        return map;
    }

}
